<!--勘探点信息列表-->
<template>
  <div class="app-container horizontal">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="90px" :model="filters">
        <el-form-item label="勘探点编号" prop="explorationCode">
          <el-input
            v-model="filters.explorationCode"
            size="small"
            placeholder="请输入勘探点编号"
            clearable
          />
        </el-form-item>
        <el-form-item label="勘探点类型" prop="explorationType">
          <el-select
            v-model="filters.explorationType"
            size="small"
            filterable
            placeholder="请选择勘探点类型"
            clearable
          >
            <el-option
              v-for="dict in explorationType"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="勘探点状态" prop="explorationStatus">
          <el-select
            v-model="filters.explorationStatus"
            size="small"
            filterable
            placeholder="请选择勘探点状态"
            clearable
          >
            <el-option
              v-for="dict in ExplorationStatusList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button id="Query" v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button id="ResetQuery" v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      <el-button icon="el-icon-edit" size="middle" :disabled="single" @click="handleUpdate">修改</el-button>
      <el-button icon="el-icon-delete" size="middle" :disabled="single" @click="handleDelete">删除</el-button>
      <el-button id="Detail" icon="el-icon-zoom-in" size="middle" :disabled="single" @click="handleDetail">详情</el-button>
      <FileUplodeBtn />
      <el-button icon="el-icon-download" size="middle" @click="handleDownload">下载模板</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData"
      :row-style="rowClass"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <el-table-column type="index" label="序号" width="51" align="center" />
      <el-table-column
        prop="explorationCode"
        label="勘探点编号"
        min-width="160"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="explorationType"
        label="勘探点类型"
        min-width="150"
        align="center"
        :show-overflow-tooltip="true"
        :formatter="formatExplorationType"
      />
      <el-table-column
        prop="x"
        label="坐标经度"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="y"
        label="坐标纬度"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="x1"
        label="坐标X"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="y1"
        label="坐标Y"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="elevation"
        label="高程(m)"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="depth"
        label="设计深度(m)"
        min-width="100"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        prop="startTime"
        label="计划开孔时间"
        align="center"
        min-width="150"
      />
      <el-table-column
        prop="endTime"
        label="计划终孔时间"
        align="center"
        min-width="150"
      />
    </el-table>
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 表格展示end-->

    <!--勘探点信息新增弹层start -->
    <addDialog
      ref="addDialog"
      :pro-id="filters.proId"
    />
    <!--勘探点信息新增弹层end -->
  </div>
</template>
<script>
// 页面接口
import { listExploration, deleteExploration, getExplorationExcelTemplate } from '@/api/projectManage/project/exploration'
// 弹框组件
import addDialog from './components/addDialog.vue'
import { mapState } from 'vuex'
import FileUplodeBtn from './components/FileUplodeBtn'
export default {
  components: {
    addDialog,
    FileUplodeBtn
  },
  data() {
    return {
      personTypeName: '', // 人员类型名称
      /* 搜索条件 start*/
      highLevelSet: false, // 高级设置显示隐藏
      filters: { // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        proId: '', // 项目id
        explorationType: '', // 勘探点类型
        explorationCode: '', // 勘探点编号
        explorationStatus: '1'// 勘探点状态
      },
      /* 搜索条件 end*/
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [], // 列表数据
      /* 列表 end*/
      status: '', // 项目状态
      address: '', // 项目地址
      province: []
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      explorationType: state => state.basicInfo.explorationType, // 勘探点类型全部
      ExplorationStatusList: state => state.basicInfo.ExplorationStatusList // 勘探点状态
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    this.filters.proId = this.$store.state.basicInfo.proId
    this.status = this.$store.state.basicInfo.proStatus
    this.address = this.$route.query.address
    this.province = this.$route.query.province
    this.getList()
  },
  methods: {
    // 下载模板
    async handleDownload() {
      const res = await getExplorationExcelTemplate()
      if (!res) return
      this.downloadFileFlow(res, '勘探点模板')
    },
    // 翻译勘探点类型
    formatExplorationType(row) {
      if (row.explorationType) return this.selectDictLabel(this.explorationType, row.explorationType)
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    // 获取列表
    getList() {
      this.loading = true
      listExploration(this.filters).then(res => {
        this.loading = false
        if (res) {
          this.tableData = res.data.rows
          this.total = res.data.total
          // this.addTaleData()
        } else {
          this.msgError(res.message)
        }
      })
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 列表 start*/
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.single = false
      } else {
        this.single = true
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 删除
    handleDelete() {
      // 获取选中行的id
      this.$confirm('确认要删除此勘探点吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          const id = this.multipleSelection[0].id
          deleteExploration(id).then(res => {
            this.loading = false
            if (res) {
              this.msgSuccess('删除成功！')
              // 重新加载表格数据
              this.getList()
            } else {
              this.msgError(res.message)
            }
          })
        }).catch(() => {
          this.msgWarning('已取消删除')
        })
    },
    /* 列表 end*/

    // 新增
    handleAdd() {
      if (this.$route.query.isRepair === true) {
        this.$refs.addDialog.handleAdd('新增项目补勘探点', '', this.province, this.address)
      } else {
        this.$refs.addDialog.handleAdd('新增项目勘探点', '', this.province, this.address)
      }
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改项目勘探点', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      const id = this.multipleSelection[0].id
      const { proId } = this.$store.state.basicInfo
      this.$router.push({
        path: '/newProject/exploration-detail',
        query: { explorationId: id, proId }
      })
    }
  }
}
</script>

